#ifndef __MSG_PACK_H__
#define __MSG_PACK_H__
#include <vector>
#include "nfevents.h"
#include "linkedlist.h"

#if defined(_DEBUG)
#define DbgPrint(format, ...)                          \
    {                                                  \
        printf("%s:%d->"##format##"\n",                \
               __FUNCTION__, __LINE__, ##__VA_ARGS__); \
    }
#else
#define DbgPrint(format, ...)
#endif

#define TCP_PACKET_SIZE 8192

struct DATA_PACKET
{
    DATA_PACKET()
    {
        buffer.len = 0;
        buffer.buf = NULL;
    }
    DATA_PACKET(char *buf, int len)
    {
        Set(buf, len);
    }
    void Set(char* buf, int len)
    {
        if (len > 0)
        {
            buffer.buf = new char[len + 1];
            buffer.len = len;

            if (buffer.buf)
            {
                memset(buffer.buf, 0, len + 1);
            }
            if (buf)
            {
                memcpy(buffer.buf, buf, len);
            }
        }
        else
        {
            buffer.buf = NULL;
            buffer.len = 0;
        }
    }
    WSABUF &operator()()
    {
        return buffer;
    }

    void free()
    {
        if (buffer.buf)
        {
            delete[] buffer.buf;
        }
        buffer.buf = NULL;
        buffer.len = 0;
    }

    WSABUF buffer;
};

typedef std::vector<DATA_PACKET> tPacketList;

enum OV_TYPE
{
    OVT_INVALID,
    OVT_ACCEPT,
    OVT_CONNECT,
    OVT_CLOSE,
    OVT_SEND,
    OVT_RECEIVE,
    OVT_UDP_SEND,
    OVT_UDP_RECEIVE
};

typedef struct OV_DATA
{
    OV_DATA()
    {
        memset(&olp, 0, sizeof(olp));
        InitializeListHead(&entry);
        id = 0;
        socket = 0;
        dwTransferred = 0;
        error = 0;
        type = OVT_INVALID;
        packet.free();

    }
    ~OV_DATA()
    {
        packet.free();
    }

    LIST_ENTRY	entry;
    ENDPOINT_ID id;
    OVERLAPPED olp;
    SOCKET socket;
    OV_TYPE type;
    DATA_PACKET packet;
    DWORD dwTransferred;
    DWORD error;
} OV_DATA, *POV;
#endif // !__MSG_PACK_H__